package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.pojo.domain.User;
import com.itheima.commons.pojo.entity.Log;
import com.itheima.commons.pojo.entity.UserInfos;
import com.itheima.dubbo.interfaces.ManageUserApi;
import com.itheima.dubbo.mysql.mapper.LoginLogMapper;
import com.itheima.dubbo.mysql.mapper.UserInfosMapper;
import com.itheima.dubbo.mysql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author wds
 * @date 2021/12/18
 * @apiNote
 */
@Service
public class ManageUserApiImpl implements ManageUserApi {
    @Autowired
    UserInfosMapper userInfosMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    LoginLogMapper loginLogMapper;

    @Override
    public List<UserInfos> queryUserInfo(String uid, String nickname, String city, Integer page, Integer pagesize) {

        Page<UserInfos> userInfoPagess = userInfosMapper.selectPage(new Page<>(page,pagesize),Wrappers.lambdaQuery(UserInfos.class)
                .like(StrUtil.isNotBlank(city),UserInfos::getCity,city)
                .like(StrUtil.isNotBlank(uid), UserInfos::getUserId, uid)
                .like(StrUtil.isNotBlank(nickname),UserInfos::getNickName, nickname));
        return userInfoPagess.getRecords();
    }

    @Override
    public User queryUser(Long uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public Log queryLog(Long uid) {
        Log log = loginLogMapper.selectById(uid);
        return log;
    }

    @Override
    public UserInfos queryUserInfoById(String userId) {
        return userInfosMapper.selectById(userId);
    }

    /**
     * 查询匹配数
     *
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @Override
    public Integer counts(String id, String nickname, String city) {
        return userInfosMapper.selectCount(Wrappers.lambdaQuery(UserInfos.class)
                .like(StrUtil.isNotBlank(city),UserInfos::getCity,city)
                .like(StrUtil.isNotBlank(id), UserInfos::getUserId, id)
                .like(StrUtil.isNotBlank(nickname),UserInfos::getNickName, nickname));
    }
}
